<?php
// 永不超时 -- cli 不能用 nginx apache
set_time_limit(0);
include __DIR__.'/function.php';
require __DIR__.'/vendor/autoload.php';
use QL\QueryList;

// 连接数据库
$db = new PDO("mysql:host=127.0.0.1;dbname=zfw","root","root");
// 添加的sql预处理
$sql = "insert into zfw_articles (title,desn,pic,url,created_at,body) values (?,?,?,?,?,'')";
$stmt = $db->prepare($sql);

$rang = range(1,7);

foreach($rang as $page){
    $url = 'https://news.ke.com/bj/baike/033/pg'.$page.'/';
    $html = http_request($url);
    // 分析采集到的内容
    $datalist = QueryList::Query($html,[
        "pic" => ['.lj-lazy','data-original','',function($item){
            // 得到扩展名
            $ext = pathinfo($item,PATHINFO_EXTENSION);
            // 生成文件名
            $filename = md5($item).'_'.date("Y-m-d_H-i-s").'.'.$ext;
            // 生成本地路径
            $filepath = dirname(__DIR__).'/public/uploads/spider/'.$filename;
            file_put_contents($filepath,http_request($item));
            return '/uploads/spider/'.$filename;
        }],
        "title" => ['.item .text .LOGCLICK','text'],
        "desn" => ['.item .text .summary','text'],
        "url" => ['.item .text > a','href'],
        "time" => ['.item .text .info .time','text']
    ])->data;

    // 入库
    foreach($datalist as $val){

        // 判断标题是否存在，存在则不进行入库

        extract($val);
        // 入库
        $stmt->execute([$title,$desn,$pic,$url,$time]);
    }
}
